clear all
clc
close all

load basicgame

%% Parameter grid

tau=0:0.1:1.5;

%% Run cross-validation

levR{1}=1/3*ones(1,3);
levC{1}=1/3*ones(1,3);

K=10;
n=length(y);
c = cvpartition(n,'KFold',K);
besttau=cell(1,K);
ErrTL=zeros(1,K);
ErrPCHM=zeros(1,K);
VarDiff=zeros(1,K);
empdistr=cell(1,K);

for k=1:K
    
    % Training Data
    
    train=find(c.training(k));
    trainy=y(train,:);
    trainx=x(train);
    idTrain=id(train);
    
    besttau{k}=CH(tau,trainy,Row,Column,idTrain);
    
     % Table Lookup
    
    idTrain=id(train);
    r=length(unique(id));
    empdistr{k}=zeros(r,3);
    if r~=466
        'WARNING'
    else
        for i=1:r
            gamesemp=find(idTrain==i);
            actemp=trainy(gamesemp);
            empdistr{k}(i,:)=[length(find(actemp=='1')),length(find(actemp=='2')),length(find(actemp=='3'))];
            empdistr{k}(i,:)=empdistr{k}(i,:)/sum(empdistr{k}(i,:));
            clear gamesemp
        end
    end      
    
    % Test Data
    
    test=find(c.test(k));
    testy=y(test,:);
    testRow=x(test);
    testCol=col(test);
    idTest=id(test);
    
    T=besttau{k};
    [ErrTL(k),ErrPCHM(k),VarDiffPCHM(k),VarDiffU(k),Covar(k)]=combinederrPCHM(tau(T(1)),Row,Column,testy,idTest,empdistr{k});
end

'Best Tau'
besttau{1} %vary to see best-fit estimates of tau in different folds of CV

'PCHM Error'
mean(ErrPCHM)

'TL Error'
mean(ErrTL)

'Completeness'
kappa=(-log(1/3)-mean(ErrPCHM))/(-log(1/3)-mean(ErrTL))

'Standard Error, Completeness'
se=sqrt((mean(VarDiffPCHM) - 2*kappa*mean(Covar) + kappa^2*mean(VarDiffU)) / (-log(1/3)-mean(ErrTL))^2) * (1/sqrt(n))



